package com.example.myapplication.activity;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.R;
import com.example.myapplication.adapter.BannerAdapter;
import com.example.myapplication.adapter.ShopAdapter;
import com.example.myapplication.bean.ShopBean;
import com.example.myapplication.utils.Constant;
import com.example.myapplication.utils.JsonParse;
import com.example.myapplication.views.ShopListView;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FirstFragment extends Fragment {

    private TextView tv_back, tv_title;         //返回键与标题控件
    private ShopListView slv_list;              //列表控件
    private ShopAdapter adapter;                //列表的适配器
    private ViewPager viewPager;
    private RelativeLayout rl_title_bar;
    public static final int MSG_SHOP_OK = 1; //获取数据
    private MHandler mHandler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        viewPager = view.findViewById(R.id.viewPager);
        init(view);
        return view;
    }

    private void init(View view) {
        tv_back = view.findViewById(R.id.tv_back);
        tv_title = view.findViewById(R.id.tv_title);
        rl_title_bar = view.findViewById(R.id.title_bar);
        slv_list = view.findViewById(R.id.slv_list);
        adapter = new ShopAdapter(requireActivity());
        slv_list.setAdapter(adapter);

        tv_title.setText("店铺");
        rl_title_bar.setBackgroundColor(requireActivity().getResources().getColor(R.color.yellow2_color));
        tv_back.setVisibility(View.GONE);

        mHandler = new MHandler();
        initData();

        // 广告栏初始化
        int[] bannerImages = {R.drawable.banner1, R.drawable.banner2, R.drawable.banner3, R.drawable.banner4, R.drawable.banner5};
        BannerAdapter bannerAdapter = new BannerAdapter(requireActivity(), bannerImages);
        viewPager.setAdapter(bannerAdapter);
    }

    private void initData() {
        try {
            // 获取 raw 资源的输入流
            InputStream is = getResources().openRawResource(R.raw.shop_list_data);
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            String res = new String(buffer, "utf-8");
            Message msg = new Message();
            msg.what = MSG_SHOP_OK;
            msg.obj = res;
            mHandler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class MHandler extends Handler {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case MSG_SHOP_OK:
                    if (msg.obj != null) {
                        String vlResult = (String) msg.obj;
                        List<ShopBean> pythonList = JsonParse.getInstance().getShopList(vlResult);
                        adapter.setData(pythonList);
                    }
                    break;
            }
        }
    }

    // 处理返回键事件
    protected long exitTime;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(requireActivity(), "再按一次退出我的美团", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                requireActivity().finish();
                System.exit(0);
            }
            return true;
        }
        return false;
    }
}
